<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form ref="formValidate" v-loading="fullscreenLoading" class="formValidate mt20" :rules="ruleValidate" :model="formValidate" label-width="120px" @submit.native.prevent>
        <el-row :gutter="24">
          <el-row v-bind="grid2">
            <el-form-item label="站点名称：" prop="siteName">
              <el-input v-model="formValidate.siteName" maxlength="149" placeholder="请输入站点名称" :disabled="isDisabled" class="selWidthd"/>
            </el-form-item>
          </el-row>
          <el-row v-bind="grid2">
            <el-form-item label="浏览器图标：" prop="image">
                <div class="tips">请上传浏览器图标，ico类型</div>
                <el-tooltip
                  class="item"
                  effect="dark"
                  content="上传图片"
                  placement="top-start"
                >
                  <el-upload
                    class="upload-demo"
                    action
                    accept="image/x-icon"
                    :before-upload="onBeforeUpload"
                    :http-request="handleUploadForm"
                    :on-change="imgSaveToUrl"
                    :headers="myHeaders"
                    :show-file-list="false"
                  >
                    <el-button
                      icon="el-icon-plus"
                      class="mr15 definedArea"
                    ></el-button>
                  </el-upload>
                </el-tooltip>
            </el-form-item>
          </el-row>

<!--          <el-row v-bind="grid2">-->
<!--            <el-form-item label="浏览器图标：" prop="image">-->
<!--              <div class="upLoadPicBox" @click="modalPicTap()" :disabled="isDisabled">-->
<!--                <div v-if="formValidate.image" class="pictrue"><img :src="formValidate.image"></div>-->
<!--                <div v-else class="upLoad">-->
<!--                  <i class="el-icon-camera cameraIconfont" />-->
<!--                </div>-->
<!--              </div>-->
<!--            </el-form-item>-->
<!--          </el-row>-->
          <el-row v-bind="grid2">
            <el-form-item label="版本信息：" prop="siteVersion">
              <el-input v-model="formValidate.siteVersion" placeholder="请输入版本信息" :disabled="isDisabled" class="selWidthd"/>
            </el-form-item>
          </el-row>
        </el-row>
        <el-form-item>
          <el-button type="primary" class="submission" @click="handleSubmit('formValidate')">提交</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  import Tinymce from '@/components/Tinymce/index'
  import { clearTreeData } from '@/utils/ZBKJIutil'
  import Templates from "../../appSetting/wxAccount/wxTemplate/index";
  import {Debounce} from '@/utils/validate'
  import {getToken} from "@/utils/auth";
  const defaultObj = {
    image: '',
    siteName: '',
    siteVersion: '',
  }

  export default {
    //name: 'ProductProductAdd',
    components: {Templates },
    data() {
      return {
        isDisabled: this.$route.params.isDisabled==='1'?true:false,
        fullscreenLoading: false,
        formValidate: Object.assign({}, defaultObj),
        //el-col响应式布局
        grid2: {
          xl: 12,
          lg: 12,
          md: 12,
          sm: 24,
          xs: 24
        },
        grid: {
          xl: 8,
          lg: 8,
          md: 12,
          sm: 24,
          xs: 24
        },
        isBtn: false,
        ruleValidate: {
          siteName: [
            { required: true, message: '请输入站点名称', trigger: 'blur' }
          ],
          image: [
            { required: true, message: '请上传浏览器图标', trigger: 'change' }
          ],
          siteVersion: [
            { required: true, message: '请输入版本信息', trigger: 'blur' }
          ],
        },
        tempRoute: {},
        myHeaders: { "X-Token": getToken() },
        localFile: null,
      }
    },
    computed: {
    },
    watch: {
    },
    created() {
      this.tempRoute = Object.assign({}, this.$route)
    },
    mounted() {
      //this.setTagsViewTitle()
      this.getInfo()
    },
    methods: {
      onBeforeUpload(file)
      {
        const isIMAGE = file.type === 'image/x-icon';
        const isLt1M = file.size / 1024 / 1024 < 1;

        if (!isIMAGE) {
          this.$message.error('上传文件只能是ico格式!');
        }
        if (!isLt1M) {
          this.$message.error('上传文件大小不能超过 1MB!');
        }
        return isIMAGE && isLt1M;
      },
      // 上传
      handleUploadForm(param) {
        const formData = new FormData();
        formData.append("multipart", param.file);
        let loading = this.$loading({
          lock: true,
          text: "上传中，请稍候...",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)",
        });

        /*fileImageApi(formData)
          .then((res) => {*/
        //图片上传接口 后期调用

        loading.close();
        this.$message.success("上传成功");
        /*})
        .catch((res) => {
          loading.close();
        });*/
      },
      // 选取图片后自动回调，里面可以获取到文件
      imgSaveToUrl(event) {
        // 也可以用file
        this.localFile = event.raw; // 或者 this.localFile=file.raw

        // 转换操作可以不放到这个函数里面，
        // 因为这个函数会被多次触发，上传时触发，上传成功也触发
        let reader = new FileReader();
        reader.readAsDataURL(this.localFile); // 这里也可以直接写参数event.raw

        // 转换成功后的操作，reader.result即为转换后的DataURL ，
        // 它不需要自己定义，你可以console.integralLog(reader.result)看一下
        reader.onload = () => {
          // console.integralLog(reader.result)
        };

        /* 另外一种本地预览方法 */
        let URL = window.URL || window.webkitURL;
        this.formValidate.image = URL.createObjectURL(event.raw);
        // 转换后的地址为 blob:http://xxx/7bf54338-74bb-47b9-9a7f-7a7093c716b5
      },

      /*setTagsViewTitle() {
        const title = this.isDisabled?'商品详情':'编辑商品'
        const route = Object.assign({}, this.tempRoute, { title: `${title}-${this.$route.params.id}` })
        this.$store.dispatch('tagsView/updateVisitedView', route)
      },*/

      // 详情
      getInfo () {
        this.fullscreenLoading = true
        //productDetailApi(this.$route.params.id).then(async res => {
        //后期接口调用
          // this.isAttr = true; //本身注释
          let res = {"image":"https://api.java.crmeb.net/crmebimage/public/content/2021/11/09/148fffc1ede348e389b3084c6886e006kvrdmkwo23.jpg","siteName":"军事训练支持系统","siteVersion":"1.0.0"};

          let info = res
          this.formValidate = {
            image: this.$selfUtil.setDomain(info.image),
            siteName: info.siteName,
            siteVersion: info.siteVersion,
          }

          this.fullscreenLoading = false
        /*}).catch(res => {
          this.fullscreenLoading = false
          this.$message.error(res.message);
        });*/
      },
      // 点击图标
      modalPicTap (i,status) {
        const _this = this;
        if(_this.isDisabled) return;
        this.$modalUpload(function(img) {
          _this.formValidate.image = img[0].attDir
        },'1', 'content')
      },

      // 提交
      handleSubmit:Debounce(function(name) {
        this.$refs[name].validate((valid) => {
          if (valid) {
            this.fullscreenLoading = true

            this.$store.commit('app/SET_SITENAME', this.formValidate.siteName)
            //productUpdateApi(this.formValidate).then(async res => {
              this.$message.success('编辑成功');
              setTimeout(() => {
                this.$router.push({ path: '/operation/siteConfig' });
              }, 500);
              this.fullscreenLoading = false
            /*}).catch(res => {
              this.fullscreenLoading = false
            })*/
          } else {
            if(!this.formValidate.siteName || !this.formValidate.siteVersion || !this.formValidate.image){
              this.$message.warning("请填写完整系统设置信息！");
            }
          }
        });
      }),
      // 表单验证
      validate (prop, status, error) {
        if (status === false) {
          this.$message.warning(error);
        }
      },
    }
  }
</script>
<style scoped lang="scss">
  .el-form-item{
    margin:0 auto 30px auto;
  }
  .definedArea{
    width:125px !important;
    height:125px !important;
    border:1px #dcdfe6 dashed !important;
  }
  .tips{
    color: #c0c4cc;
    font-size:14px;
  }
  .disLabel{
    ::v-deep.el-form-item__label{
      margin-left: 36px !important;
    }
  }
  .disLabelmoren{
    ::v-deep.el-form-item__label{
      margin-left: 120px !important;
    }
  }
  .priamry_border{
    border: 1px solid #1890FF;
    color: #1890FF;
  }
  .color-item{
    height: 30px;
    line-height: 30px;
    padding: 0 10px;
    color:#fff;
    margin-right :10px;
  }
  .color-list .color-item.blue{
    background-color: #1E9FFF;
  }
  .color-list .color-item.yellow{
    background-color: rgb(254, 185, 0);
  }
  .color-list .color-item.green{
    background-color: #009688;
  }
  .color-list .color-item.red{
    background-color: #ed4014;
  }
  .proCoupon{
    ::v-deep.el-form-item__content{
      margin-top: 5px;
    }
  }
  .tabPic{
    width: 40px !important;
    height: 40px !important;
    img{
      width: 100%;
      height: 100%;
    }
  }
  .noLeft{
    ::v-deep.el-form-item__content{
      margin-left: 0 !important;
    }
  }
  .tabNumWidth{
    ::v-deep.el-input-number--medium{
      width: 121px !important;
    }
    ::v-deep.el-input-number__increase{
      width: 20px !important;
      font-size: 12px !important;
    }
    ::v-deep.el-input-number__decrease{
      width: 20px !important;
      font-size: 12px !important;
    }
    ::v-deep.el-input-number--medium .el-input__inner {
      padding-left: 25px !important;
      padding-right: 25px !important;
    }
    ::v-deep thead{
      line-height: normal !important;
    }
    ::v-deep .el-table .cell{
      line-height: normal !important;
    }
  }
  .selWidth{
    width: 100%;
  }
  .selWidthd{
    width: 300px;
  }
  .button-new-tag {
    height: 28px;
    line-height: 26px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
  .pictrue{
    width: 60px;
    height: 60px;
    border: 1px dotted rgba(0,0,0,0.1);
    margin-right: 10px;
    position: relative;
    cursor: pointer;
    img{
      width: 100%;
      height: 100%;
    }
    video{
      width: 100%;
      height: 100%;
    }
  }
  .btndel{
    position: absolute;
    z-index: 1;
    width :20px !important;
    height: 20px !important;
    left: 46px;
    top: -4px;
  }
  .labeltop{
    ::v-deep.el-form-item__label{
      float: none !important;
      display: inline-block !important;
      width: auto !important;
    }
  }
  .iview-video-style {
  width: 300px;
  height: 180px;
  border-radius: 10px;
  background-color: #707070;
  margin: 0 120px 20px;
  position: relative;
  overflow: hidden;
}

.iview-video-style .iconv {
  color: #fff;
  line-height: 180px;
  width: 50px;
  height: 50px;
  display: inherit;
  font-size: 26px;
  position: absolute;
  top: -74px;
  left: 50%;
  margin-left: -25px;
}

.iview-video-style .mark {
  position: absolute;
  width: 100%;
  height: 30px;
  top: 0;
  background-color: rgba(0, 0, 0, 0.5);
  text-align: center;
}
</style>
